#!/usr/bin/env sh

D="clusterd statd netd mond statd syncd lockd hbd cldb clstatdb"

L="$1"

if [ "X$L" = X ]
then
	echo "Usage: lha_set_log_level.sh [-a] debug|verbose|quiet|silent" >&2
	exit 1
fi

all=0
if [ "$L" = "-a" ]
then
	all=1
	L="$2"
fi

if [ "X$L" = X ]
then
	echo "Usage: lha_set_log_level.sh [-a] debug|verbose|quiet|silent" >&2
	exit 1
fi

ok=`echo $L | egrep '^(debug|verbose|quiet|silent)$'`

if [ "X$ok" = X ]
then
	echo "Usage: lha_set_log_level.sh debug|verbose|quiet|silent" >&2
	exit 1
fi

master=`clreq -a get_master_node | awk '$1 ~ /:OK$/ {print $2;exit;}'`

if [ "X$master" = X ]
then
	echo "Error: Unable to determine master node in cluster - is it up?" >&2
	exit 1
fi

echo "Master node: $master"

nodes=`hostname`
if [ $all = 1 ]
then
	nodes=`clreq -h crxsashpdb01 perform_adhoc_sql dbtype status sql "SELECT var FROM cluster_status_vars WHERE value = 'Joined';" | awk -v FS=: 'NR>1 {print $1}'`
	if [ "X$nodes" = X ]
	then
		echo "No nodes found in cluster - is it up?" >&2
		exit 1
	fi
fi

for host in $nodes
do
	echo "NODE $host: \c"
	for daemon in $D
	do
		echo "$daemon: \c"
		resp=`lha_daemonctl --node $host --daemon $daemon --msg "SET_LOG_LEVEL $L"`
		echo "$resp \c"
	done
	echo
done
exit 0
